package de.tabuma.sso.user;

import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class UserController extends tabumacontroller {

    private static final Logger log = Logger.getLogger(UserController.class.getName());

    public UserController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            String username = "username";
            String password = "password";
            String entityName = "user";

            String pageHead = "User Management";

            out.println(view.getContentHeader(pageHead));

            Content content = new Content();
            
            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(de.tabuma.sso.user.User.class.getDeclaredField("name"));
            editableFields.add(de.tabuma.sso.user.User.class.getDeclaredField("password"));
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(1));
            editableTypes.add(new Integer(2));
            content.FormContent.setEditableFieldTypes(editableTypes);
            
            content.FormContent.setEntityClass(de.tabuma.sso.user.User.class);

            content.FormContent.setEntityName(entityName);
            
            content.FormContent.setFacadeObject(new UserFacade());

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();
            content.ListContent.setListParameterValue(user.getEmail());

            content.GeneralContent.setParamName("userNav");

            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(de.tabuma.sso.user.User.class.getDeclaredField("name"));
            content.ListContent.setListableFields(listableFields);

            content.GeneralContent.setServlet(this.getServlet());

            content.GeneralContent.setParameter(this.getParameter());

            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(de.tabuma.sso.user.User.class.getDeclaredField("name"), username);
            displayFields.put(de.tabuma.sso.user.User.class.getDeclaredField("password"), password);

            content.FormContent.setDisplayedName(displayFields);

            content.GeneralContent.setUser(user.getEmail());

            out.println(content.navigate(request));
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}